﻿using Dapper;
using Domain.Entity;
using Domain.Entity.sys;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class CodeRuleRepository : ICodeRuleRepository
    {

        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加编号规则信息
        public async Task<int> AddCodeRule(CodeRule codeRule)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[sys_code_rule](code_name, code_prefix, timerule, code_num, code_step, numberrule, create_name, create_time)
                                values('{codeRule.code_name}','{codeRule.code_prefix}','{codeRule.timerule}',{codeRule.code_num},{codeRule.code_step},'{codeRule.numberrule}','{codeRule.create_name}','{codeRule.create_time}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //删除编号规则信息
        public async Task<int> DeleteCodeRule(int id)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"update [dbo].[sys_code_rule] set is_delete=1 where code_id={id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //生成编号规则
        public async Task<string> GeneratedCode(int id)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                //生成编号存储过程
                DynamicParameters param = new DynamicParameters();
                param.Add("@code_id", id);
                param.Add("@result", dbType: DbType.String, direction: ParameterDirection.Output, size: 64);
                string sql = "exec [dbo].[procBianHao] @code_id, @result output";
                await sqlConnection.ExecuteAsync(sql, param);
                // 获取输出参数的值
                string result = param.Get<string>("@result");
                return result;
            }
        }


        //分页查询编号规则信息
        public async Task<FenYe<CodeRule>> GetCodeRule(FenYe<CodeRule> fenYe,string? name, string? prefix, string? timerule, string? starttime, string? endtime)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();

                // 分页存储过程
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", 10);
                param.Add("@keyName", "code_id");
                param.Add("@tabelName", "sys_code_rule");
                param.Add("@currentPage", fenYe.CurrentPage);
                param.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                //判断条件
                string sqlparam = $" is_delete=0 ";
                if (!string.IsNullOrEmpty(name))
                    sqlparam += $" and code_name like '%{name}%'";
                if (!string.IsNullOrEmpty(prefix))
                    sqlparam += $" and code_prefix like '%{prefix}%'";
                if (!string.IsNullOrEmpty(timerule))
                    sqlparam += $" and timerule like '%{timerule}%'";
                if (!string.IsNullOrEmpty(starttime) || !string.IsNullOrEmpty(endtime))
                    sqlparam += $" and  create_time between '{starttime}' and '{endtime}'";

                param.Add("@where", sqlparam);

                string sql = "exec [dbo].[Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output";
                List<CodeRule> list = sqlConnection.Query<CodeRule>(sql, param).ToList();
                fenYe.List = list;
                fenYe.Rows = param.Get<int>("@rows");
                return fenYe;
            }
        }

        //根据id查询编号规则信息
        public async Task<CodeRule> GetCodeRuleByid(int id)
        {
            using (SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"select * from [dbo].[sys_code_rule] where code_id={id}";
                return await sqlConnection.QueryFirstAsync<CodeRule>(sql);
            }
        }

        //修改编号规则信息
        public async Task<int> UpdateCodeRule(CodeRule codeRule)
        {
            using (SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[sys_code_rule] set code_name='{codeRule.code_name}',code_prefix='{codeRule.code_prefix}',timerule='{codeRule.timerule}',code_num={codeRule.code_num},code_step={codeRule.code_step},numberrule='{codeRule.numberrule}',update_name='{codeRule.update_name}',update_time='{codeRule.update_time}' where code_id={codeRule.code_id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
